globals

 

globals keeps track of information that should be common to the entire model. In the same way that the value of a slider can be accessed anywhere in NetLogo code, any bit of your code can both set (using the set command) and read (by typing the name of the global) the value of a global. This fact makes globals useful for defining model-wide constants that you don't want the user to be able to change from a slider. It also makes them useful for keeping track of variable model-wide information that any agent can modify, for instance, the total accumulated volume of transactions in an economy model or the number of times that a predator caught their prey in an ecological model.

In this example, a global is used to keep track of a model-wide variable, the total patches of grass eaten by all the 20 sheep in the model. In the setup procedure, we set it to 0, and then in the go procedure, each time a sheep eats a patch of grass, we increment it by one by setting its value to what its value used to be plus one, i.e. set total-food-eaten total-food-eaten + 1.

 

Try it Yourself

 
 
 
 
 
 
 

What's next?

Once you mastered the globals primitive, don't stop there. Check out the resources below to improve your NetLogo skills.

 
Similar primitives:
turtles-own

Declare a variable that belongs to turtles.

Read more
patches-own

Declare a variable that belongs to patches.

Read more
set

Set the value of a variable.

Read more
if-else

execute specific code if a condition is true, otherwise, execute other code

Read more
 
Learn another primitive